<?php

class UsersController extends Zend_Controller_Action
{

	protected $_redirector = null;
    public function init()
    {

    	$contextSwitch = $this->getHelper('contextSwitch');
    	$contextSwitch->addActionContext('edit', 'json')
    				  ->initContext();
    }

    public function indexAction()
    {
    }

    public function loginAction()
    {
				/*
				if(Zend_Auth::getInstance()->hasIdentity()){

					$this->_redirect('/account/index');
				}*/

				$this->view->headTitle('Login ', 'PREPEND');
				$this->view->message =  implode(" ", $this->_helper->flashMessenger->getMessages());
			        /* Initialize action controller here */
					$request = $this->getRequest();
					$form = new Form_Login();
				if($request->isPost()) {
						if ($form->isValid($this->getRequest()->getPost()))	{

							$authAdapter = $this->getAuthAdapter();
							$username = $form->getValue('username');
							$password = $form->getValue('password');

							$authAdapter->setIdentity($username)
										->setCredentialTreatment('md5(?)')
										->setCredential($password);

							//Getting an instance of Zend_Auth
							$auth = Zend_Auth::getInstance();
							$result = $auth->authenticate($authAdapter);

							if ($result->isValid()){

								//Get row object of identity related to this user
								$identity = $authAdapter->getResultRowObject();
								//Get session wide storage
								$authStorage = $auth->getStorage();
								$authStorage->write($identity);
								$forward = $this->_request->getPost('return');
								$this->view->errorMessage=$forward;
      							 $this->_redirector = $this->_helper->getHelper('Redirector');
      							 //$this->_redirector->setUseAbsoluteUri($forward);
       							$this->_redirect($forward);								//$flashRedirector = $this->_helper->flashMessenger->
								//$this->_forward($forward);
							} else {

								$this->view->errorMessage = "Username or Password invalid";
									}
						}

		}
		//Send the form to the view
		$this->view->form = $form ;

	}

    public function registerAction()
    {
	    $this->view->title = "Register a new User ";
		$this->view->headTitle($this->view->title, 'PREPEND');
		$form = new Form_Register();
		//$form->submit->setLabel('Register');
		$this->view->form = $form;
		if ($this->getRequest()->isPost()) {
			$formData = $this->getRequest()->getPost();
		if ($form->isValid($formData)) {
			$fname = $form->getValue('fname');
			$lname = $form->getValue('lname');
			$email = $form->getValue('email');
			$school = $form->getValue('school');
			$uname = $form->getValue('username');
			$password = $form->getValue('password');

			$users = new Model_Users();
			$users->addUser($fname, $lname,$email, $school, $uname, $password );
			$this->_redirect('/');
		} else {
						$form->populate($formData);
				}
}
}


public function logoutAction() {

	Zend_Auth::getInstance()->clearIdentity();
}

/**
 * Function: editAction
 * This function is used for editing a particular user's records
 */
public function editAction () {

		$this->view->title = "Edit your records";
		$form = new Form_Register();
		$form->submit->setLabel('Edit');
		$this->view->form = $form;
		if ($this->getRequest()->isPost()) {
		$formData = $this->getRequest()->getPost();
		if ($form->isValid($formData)) {
		$id = (int)$form->getValue('id');
		$fname = $form->getValue('fname');
		$lname = $form->getValue('lname');
		$email = $form->getValue('email');
		$school = $form->getValue('school');
		$uname = $form->getValue('username');
		$password = $form->getValue('password');
		$users = new Model_Users();
		$users->editAccount($id,$fname, $lname,$email, $school, $uname, $password );
		$this->view->msg = "Your records have been edited";
		} else {
		$form->populate($formData);
		}
		} else {
		$id = $this->_getParam('id', 0);
		if ($id > 0) {
		$users = new Model_Users();
		$form->populate($users->getUser($id));
		}
}
    }

public function deleteAction($id) {

	$user = new Model_Users();
	$user->deleteUser($id);
}
/**
 * Private Function
 * This gets the authentication adapter for the application
 */
private function getAuthAdapter() {

	$authAdapter = new Zend_Auth_Adapter_DbTable(Zend_Db_Table::getDefaultAdapter());
	$authAdapter->setTableName('users')
	        	->setIdentityColumn('username')
				->setCredentialColumn('password');

	return $authAdapter;
}



}



